package Day013_线程安全.ZuoYe.Test1;

import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author zhangyan
 * @date 2020/5/4 17:01
 */
public class Test1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池-初始化线程
        ExecutorService es = Executors.newFixedThreadPool(3);
        //创建任务
        MyCallable mc = new MyCallable();
        //提交并执行任务
        Future<Double> future1 = es.submit(mc);
        System.out.println(future1.get());

        Future<Double> future2 = es.submit(mc);
        System.out.println(future2.get());

        Future<Double> future3 = es.submit(mc);
        System.out.println(future3.get());

        //三个平均数的平均数
        //使用BigDecimal计算小数
        BigDecimal b1 = BigDecimal.valueOf(future1.get());
        BigDecimal b2 = BigDecimal.valueOf(future2.get());
        BigDecimal b3 = BigDecimal.valueOf(future3.get());
        //四舍五入保留5位小数
        System.out.println((b1.add(b2).add(b3)).divide(BigDecimal.valueOf(3),5,BigDecimal.ROUND_HALF_UP));
        es.shutdown();
    }
}

